<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.2">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.2" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="git是一个开源的分布式版本控制系统，可用于高效的管理大小项目。">
<meta name="keywords" content="git">
<meta property="og:type" content="article">
<meta property="og:title" content="Git">
<meta property="og:url" content="https://rencheng.cc/2016/08/25/git/Git/index.html">
<meta property="og:site_name" content="PHOENIX&#39;s Blog">
<meta property="og:description" content="git是一个开源的分布式版本控制系统，可用于高效的管理大小项目。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2016-08-25T03:17:41.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git">
<meta name="twitter:description" content="git是一个开源的分布式版本控制系统，可用于高效的管理大小项目。">



  <link rel="alternate" href="/atom.xml" title="PHOENIX's Blog" type="application/atom+xml">



  
  
  <link rel="canonical" href="https://rencheng.cc/2016/08/25/git/Git/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Git | PHOENIX's Blog</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">PHOENIX's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">Enjoy the journey of life</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://rencheng.cc/2016/08/25/git/Git/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="任成">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.JPG">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="PHOENIX's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Git

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2016-08-25 11:17:41" itemprop="dateCreated datePublished" datetime="2016-08-25T11:17:41+08:00">2016-08-25</time>
            

            
          </span>

          

          
            
            
          

          
          

          

          
            <div class="post-symbolscount">
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                <span title="本文字数">4k</span>
              

              

              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>git是一个开源的分布式版本控制系统，可用于高效的管理大小项目。</p>
<a id="more"></a>
<h2 id="代码管理工具作用"><a href="#代码管理工具作用" class="headerlink" title="代码管理工具作用"></a>代码管理工具作用</h2><ul>
<li>防止代码丢失，做备份</li>
<li>代码版本的管理，可以进行多个节点的备份，在多个版本之间跳跃</li>
<li>可以方便的将代码在多人之间进行共享传输</li>
<li>多人开发时有各种模式可以方便代码管理</li>
</ul>
<h2 id="什么是git"><a href="#什么是git" class="headerlink" title="什么是git"></a>什么是git</h2><p>git是一个开源的分布式版本控制系统，可用于高效的管理大小项目。</p>
<h2 id="分布式和集中式"><a href="#分布式和集中式" class="headerlink" title="分布式和集中式"></a>分布式和集中式</h2><p>分布式： 每个节点都保存完成的代码，没有明确的中央服务器，节点之间项目推送下载代码完成代码共享</p>
<p>集中式： 代码集中管理，每次完成的代码上传到中央管理器，然后再统一从中央管理器下载代码使用</p>
<h2 id="git特点"><a href="#git特点" class="headerlink" title="git特点"></a>git特点</h2><ul>
<li>git可以管理各种文件，特别是代码项目，多在*nix系统中使用</li>
<li>是分布式管理，不同于集中式，这是git和svn的核心区别</li>
<li>git可以更好的支持分支，方便多人协同工作</li>
<li>git分布式代码更安全，有全球唯一的commit版本号</li>
<li>git是开源的系统</li>
<li>使用git可以脱网工作，且数据传输速度较快</li>
</ul>
<h2 id="git安装"><a href="#git安装" class="headerlink" title="git安装"></a>git安装</h2><p>linux:<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-<span class="builtin-name">get</span> install git</span><br></pre></td></tr></table></figure></p>
<p>windows<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">msysgit<span class="selector-class">.github</span><span class="selector-class">.io</span></span><br></pre></td></tr></table></figure></p>
<h2 id="git-配置命令"><a href="#git-配置命令" class="headerlink" title="git 配置命令"></a>git 配置命令</h2><h3 id="git-config"><a href="#git-config" class="headerlink" title="git config"></a>git config</h3><h4 id="配置级别"><a href="#配置级别" class="headerlink" title="配置级别"></a>配置级别</h4><ol>
<li><p>系统中所有的用户都可使用该配置</p>
 <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git<span class="built_in"> config </span> --system</span><br></pre></td></tr></table></figure>
<p> 配置文件： /etc/gitconfig</p>
</li>
<li><p>当前用户可使用该配置</p>
 <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git<span class="built_in"> config </span> --global</span><br></pre></td></tr></table></figure>
<p> 配置文件： ~/.gitconfig</p>
</li>
<li><p>当前项目可使用该配置</p>
 <figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">config</span></span><br></pre></td></tr></table></figure>
<p> 配置文件： project/.git/config</p>
</li>
</ol>
<h4 id="配置内容"><a href="#配置内容" class="headerlink" title="配置内容"></a>配置内容</h4><ol>
<li>配置用户名<br> 配置用户名为rencheng： <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo git<span class="built_in"> config </span>--system user.name rencheng</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol>
<li>配置用户邮箱<br> 配置邮箱 <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git<span class="built_in"> config </span>--global user.email rencheng11@icloud.cn</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol>
<li>配置编译器 <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git<span class="built_in"> config </span>core.editor sublime</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol>
<li>查看配置信息 <figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git<span class="built_in"> config </span>--list</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="git基本命令"><a href="#git基本命令" class="headerlink" title="git基本命令"></a>git基本命令</h2><h3 id="初始化仓库"><a href="#初始化仓库" class="headerlink" title="初始化仓库"></a>初始化仓库</h3><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">git init</span></span><br></pre></td></tr></table></figure>
<ul>
<li>在某个目录下初始化仓库后会自动产生.git目录。该目录下工作的所有文档即可以使用git进行管理</li>
</ul>
<h3 id="查看分支状态"><a href="#查看分支状态" class="headerlink" title="查看分支状态"></a>查看分支状态</h3><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">git status</span></span><br></pre></td></tr></table></figure>
<ul>
<li>默认工作分支为master，可通过创建新的分支切换</li>
</ul>
<h3 id="文件提交"><a href="#文件提交" class="headerlink" title="文件提交"></a>文件提交</h3><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git  <span class="keyword">add</span><span class="bash">   [file]</span></span><br></pre></td></tr></table></figure>
<ul>
<li>将文件提交到暂存区</li>
<li>提交内容可以是一个文件，多个文件用个空格分开</li>
<li>如果是 * 表示所有文件，也可以是目录</li>
</ul>
<h3 id="删除暂存区某个文件提交记录"><a href="#删除暂存区某个文件提交记录" class="headerlink" title="删除暂存区某个文件提交记录"></a>删除暂存区某个文件提交记录</h3><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rm <span class="comment">--cached Readme.txt</span></span><br></pre></td></tr></table></figure>
<h3 id="文件同步到本地仓库"><a href="#文件同步到本地仓库" class="headerlink" title="文件同步到本地仓库"></a>文件同步到本地仓库</h3><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">git</span> commit -m <span class="string">"some message"</span></span><br></pre></td></tr></table></figure>
<ul>
<li>同步时需要附加一些同步信息 在-m后添加</li>
<li>所有对工作区的修改如果想同步到本地仓库，都需要    add—-&gt;commit</li>
</ul>
<h3 id="查看commit日志"><a href="#查看commit日志" class="headerlink" title="查看commit日志"></a>查看commit日志</h3><figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">log</span></span><br><span class="line">git <span class="built_in">log</span>  <span class="comment">--pretty=oneline</span></span><br></pre></td></tr></table></figure>
<h3 id="一些工作区命令"><a href="#一些工作区命令" class="headerlink" title="一些工作区命令"></a>一些工作区命令</h3><h4 id="查看本地文件和工作区差异"><a href="#查看本地文件和工作区差异" class="headerlink" title="查看本地文件和工作区差异"></a>查看本地文件和工作区差异</h4><pre><code>git  diff   file
</code></pre><h4 id="从本地仓库恢复文件"><a href="#从本地仓库恢复文件" class="headerlink" title="从本地仓库恢复文件"></a>从本地仓库恢复文件</h4><pre><code>git checkout file
</code></pre><h4 id="丢弃工作区修改"><a href="#丢弃工作区修改" class="headerlink" title="丢弃工作区修改"></a>丢弃工作区修改</h4><pre><code>git checkout -- file
</code></pre><h3 id="本地仓库文件的移动和删除"><a href="#本地仓库文件的移动和删除" class="headerlink" title="本地仓库文件的移动和删除"></a>本地仓库文件的移动和删除</h3><pre><code> 移动文件
 git  mv   file  dir 

 删除文件
 git  rm   file

 * 用法和mv rm命令相同。操作后直接commit即可工  作区和本地仓库同步
</code></pre><h3 id="版本控制命令"><a href="#版本控制命令" class="headerlink" title="版本控制命令"></a>版本控制命令</h3><h4 id="回到之前版本"><a href="#回到之前版本" class="headerlink" title="回到之前版本"></a>回到之前版本</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">reset</span> <span class="comment">--hard HEAD^</span></span><br></pre></td></tr></table></figure>
<ul>
<li>HEAD后的<code>^</code>数量决定了回到上几个版本</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">reset</span> <span class="comment">--hard  commit_id</span></span><br></pre></td></tr></table></figure>
<ul>
<li>使用commit前7位即可，回到指定的版本</li>
</ul>
<h4 id="去往更新的版本"><a href="#去往更新的版本" class="headerlink" title="去往更新的版本"></a>去往更新的版本</h4><ol>
<li><p>查看所有历史版本号</p>
 <figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">git reflog</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>使用git reset 去往指定版本</p>
<ul>
<li>git reflog 会有所有的操作记录，最新的操作时钟在   最上边</li>
</ul>
</li>
</ol>
<h3 id="标签管理"><a href="#标签管理" class="headerlink" title="标签管理"></a>标签管理</h3><p>即在当前工作位置添加快照，保存工作状态，一般用于版本的迭代。</p>
<h4 id="创建新的标签"><a href="#创建新的标签" class="headerlink" title="创建新的标签"></a>创建新的标签</h4><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">tag</span>  <span class="title">v1</span>.<span class="number">0</span></span><br></pre></td></tr></table></figure>
<ul>
<li>默认在最新的commit_id处打标签</li>
</ul>
<h4 id="添加标签信息"><a href="#添加标签信息" class="headerlink" title="添加标签信息"></a>添加标签信息</h4><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">tag</span> <span class="title">v1</span>.<span class="number">0</span>  -m  <span class="string">"message"</span></span><br></pre></td></tr></table></figure>
<h4 id="指定某个commit-id打标签"><a href="#指定某个commit-id打标签" class="headerlink" title="指定某个commit_id打标签"></a>指定某个commit_id打标签</h4><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">tag</span>  <span class="title">v0</span>.<span class="number">9</span>  [commit_id]</span><br></pre></td></tr></table></figure>
<h4 id="查看标签"><a href="#查看标签" class="headerlink" title="查看标签"></a>查看标签</h4><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">tag</span>    <span class="title">#列出当前标签</span></span><br><span class="line"><span class="title">git</span> show v1.<span class="number">0</span>  <span class="comment">#显示标签具体信息</span></span><br></pre></td></tr></table></figure>
<h4 id="删除标签"><a href="#删除标签" class="headerlink" title="删除标签"></a>删除标签</h4><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">tag</span> <span class="title">-d</span> v1.<span class="number">0</span></span><br></pre></td></tr></table></figure>
<h4 id="去往某个标签版本"><a href="#去往某个标签版本" class="headerlink" title="去往某个标签版本"></a>去往某个标签版本</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="keyword">reset</span> <span class="comment">--hard  v0.9</span></span><br></pre></td></tr></table></figure>
<h3 id="临时工作区操作"><a href="#临时工作区操作" class="headerlink" title="临时工作区操作"></a>临时工作区操作</h3><h4 id="创建保存临时工作区"><a href="#创建保存临时工作区" class="headerlink" title="创建保存临时工作区"></a>创建保存临时工作区</h4><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">git  stash</span></span><br></pre></td></tr></table></figure>
<h4 id="查看保存的工作区"><a href="#查看保存的工作区" class="headerlink" title="查看保存的工作区"></a>查看保存的工作区</h4><figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git  stash  <span class="built_in">list</span></span><br></pre></td></tr></table></figure>
<h4 id="应用哪个工作区"><a href="#应用哪个工作区" class="headerlink" title="应用哪个工作区"></a>应用哪个工作区</h4><figure class="highlight coq"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git stash <span class="built_in">apply</span> stash@&#123;<span class="number">1</span>&#125;</span><br></pre></td></tr></table></figure>
<h4 id="应用上一个工作区并且删除"><a href="#应用上一个工作区并且删除" class="headerlink" title="应用上一个工作区并且删除"></a>应用上一个工作区并且删除</h4><figure class="highlight avrasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git stash <span class="keyword">pop</span></span><br></pre></td></tr></table></figure>
<h4 id="删除工作区"><a href="#删除工作区" class="headerlink" title="删除工作区"></a>删除工作区</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git stash <span class="keyword">drop</span> stash@&#123;<span class="number">0</span>&#125;   <span class="comment">#删除某一个</span></span><br><span class="line">git stash <span class="keyword">clear</span>            <span class="comment">#删除所有</span></span><br></pre></td></tr></table></figure>
<h3 id="分支操作"><a href="#分支操作" class="headerlink" title="分支操作"></a>分支操作</h3><h4 id="什么是分支？"><a href="#什么是分支？" class="headerlink" title="什么是分支？"></a>什么是分支？</h4><p>分支即每个人获取原有代码，在此基础上创建自己的工作环境，单独开发，不会影响其他分支的操作。开发完成后再统一合并到主线分支中。</p>
<p>分支的好处：安全，不影响其他人工作，自己控制进度</p>
<h4 id="查看当前分支"><a href="#查看当前分支" class="headerlink" title="查看当前分支"></a>查看当前分支</h4><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git  branch</span><br><span class="line">``` </span><br><span class="line"></span><br><span class="line">* 前面有 * 号的分支表示当前正在工作的分支</span><br><span class="line"></span><br><span class="line">#### 创建分支</span><br></pre></td></tr></table></figure>
<p>git branch  [branch_name]<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 切换工作分支</span><br></pre></td></tr></table></figure></p>
<p>git checkout [branch]<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 创建并切换到新的分支</span><br></pre></td></tr></table></figure></p>
<p>git checkout -b [branch_name]<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 分支合并</span><br><span class="line"></span><br><span class="line">将某个分支合并到当前分支</span><br></pre></td></tr></table></figure></p>
<p>git merge [branch]<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">* 合并过程中如果没有冲突则直接合并后当前分支即为   干净的状态</span><br><span class="line">* 如果产生冲突则需要人为选择然后在进行add    commit等操作</span><br><span class="line">* 在创建分支前尽量保证当前分支是干净点，以减少冲突的发生</span><br><span class="line"></span><br><span class="line">#### 删除分支</span><br></pre></td></tr></table></figure></p>
<p>git branch -d [branch_name]<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 强制删除没有合并的分支</span><br></pre></td></tr></table></figure></p>
<p>git branch -D [branch_name]<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">### 远程仓库</span><br><span class="line">远程主机上的仓库。实际上git是分布式的，每一台主机的git结构都相似，只是把其他主机的git仓库叫做远程而已。</span><br><span class="line"></span><br><span class="line">#### 创建共享仓库</span><br><span class="line"></span><br><span class="line">1. 创建文件夹</span><br></pre></td></tr></table></figure></p>
<pre><code>mkdir gitrepo
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="code">    </span></span><br><span class="line"><span class="bullet">2. </span>设置文件夹属主</span><br></pre></td></tr></table></figure>

chown tarena:tarena gitrepo
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="code">    </span></span><br><span class="line"><span class="bullet">3. </span>将该文件夹设置为可共享的git仓库</span><br></pre></td></tr></table></figure>

cd gitrepo
git init  --bare  fly.git
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="code">    </span></span><br><span class="line"><span class="bullet">4. </span>设置本地仓库属主</span><br></pre></td></tr></table></figure>

chown -R tarena:tarena fly.git
<figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 添加远程仓库</span><br></pre></td></tr></table></figure>
</code></pre><p>git remote add origin <span class="exturl" data-url="aHR0cHM6Ly8qKioqLyoqKiouZ2l0" title="https://****/****.git">https://****/****.git<i class="fa fa-external-link"></i></span><br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">* 默认使用SSH作为传输手段</span><br><span class="line">* 必须在本地的某个git仓库下执行才能使本地仓库和远程   仓库关联</span><br><span class="line"></span><br><span class="line">#### 删除远程主机</span><br></pre></td></tr></table></figure></p>
<p>git remote  rm  [origin]<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 将本地分支推送到远程</span><br></pre></td></tr></table></figure></p>
<p>git push -u origin master<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* 在第一次向远程仓库推送时需要加 -u选项，以后就不   需要了</span><br><span class="line"></span><br><span class="line">#### 从远程仓库获取项目</span><br></pre></td></tr></table></figure></p>
<p>git clone <span class="exturl" data-url="aHR0cHM6Ly8qKioqLyoqKiouZ2l0" title="https://****/****.git">https://****/****.git<i class="fa fa-external-link"></i></span><br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">### 从远程仓库拉取分支或代码</span><br><span class="line">直接拉取远程分支和当前工作分支合并</span><br></pre></td></tr></table></figure></p>
<p>git pull origin dev_Tom<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">  </span><br><span class="line">拉取远程分支到本地，不合并</span><br></pre></td></tr></table></figure></p>
<p>git pull origin dev_Tom : dev_Tom<br>                远程分支名 本地分支名<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#### 代码退出和拉取</span><br><span class="line">将本地代码推送到连接的远程仓库</span><br></pre></td></tr></table></figure></p>
<p>git  push<br><figure class="highlight autohotkey"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">```</span><br><span class="line">git push  --force  origin (当本地版本比远程版本旧                            是用本地旧版本覆盖远程                            新版本)</span><br></pre></td></tr></table></figure></p>
<p>从远程仓库更新代码<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">git</span>  pull</span><br><span class="line">git  fetch  （如果有新的分支拉取到本地不会和本地              分支合并）</span><br></pre></td></tr></table></figure></p>
<h3 id="github"><a href="#github" class="headerlink" title="github"></a>github</h3><p>github是一个开源项目社区网站。拥有全球最多的开源项目。开发者可以注册这个网站建立自己的github仓库。然后就可以在本地通过git像操作远程仓库一样操作github仓库。</p>
<p>git是github唯一指定的代码管理工具</p>
<p>网址：<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tLw==" title="https://github.com/">https://github.com/<i class="fa fa-external-link"></i></span></p>
<h4 id="添加ssh秘钥"><a href="#添加ssh秘钥" class="headerlink" title="添加ssh秘钥"></a>添加ssh秘钥</h4><ol>
<li><p>在本地主机生成ssh密钥对<br>   ssh-keygen</p>
<ul>
<li>默认密钥对存放在 <code>~/.ssh/</code> 下</li>
<li>生成过程会提示设置密码，如果直接回车则表示不设置密码</li>
</ul>
</li>
<li><p>进入 <code>~/.ssh</code> 目录 复制 <code>id_rsa.pub</code> 公钥内容</p>
</li>
<li>登录github账号  <ol>
<li>右上角头像下拉菜单</li>
<li>settings</li>
<li>左侧 SSH and GPG keys</li>
<li>new ssh key</li>
<li>填写title，将复制内容加入key文本框</li>
<li>点击add…</li>
</ol>
</li>
</ol>
<h4 id="创建新的github仓库"><a href="#创建新的github仓库" class="headerlink" title="创建新的github仓库"></a>创建新的github仓库</h4><ol>
<li>右上角 + 下拉菜单</li>
<li>new repository</li>
<li>填写参考名和基本描述，根据情况选择是否添加readme等内容，选择共有还是私有</li>
<li>点击创建</li>
</ol>
<h4 id="操作github仓库"><a href="#操作github仓库" class="headerlink" title="操作github仓库"></a>操作github仓库</h4><ol>
<li>git remote 连接远程github仓库 如果需要输入密码输   入github密码即可</li>
<li>使用git push等操作远程仓库的方法操作即可</li>
</ol>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/git/" rel="tag"># git</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/09/04/life/心灵鸡汤/" rel="next" title="鸡汤">
                <i class="fa fa-chevron-left"></i> 鸡汤
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/08/27/linux/命令大全/" rel="prev" title="Linux-常用命令">
                Linux-常用命令 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.JPG" alt="任成">
            
              <p class="site-author-name" itemprop="name">任成</p>
              <div class="site-description motion-element" itemprop="description"></div>
          </div>

          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL0NvZGVyQ3VydmU=" title="GitHub &rarr; https://github.com/CoderCurve"><i class="fa fa-fw fa-github"></i>GitHub</span>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="bWFpbHRvOnJlbmNoZW5nMTFAaWNsb3VkLmNvbQ==" title="E-Mail &rarr; mailto:rencheng11@icloud.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#代码管理工具作用"><span class="nav-number">1.</span> <span class="nav-text">代码管理工具作用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是git"><span class="nav-number">2.</span> <span class="nav-text">什么是git</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分布式和集中式"><span class="nav-number">3.</span> <span class="nav-text">分布式和集中式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git特点"><span class="nav-number">4.</span> <span class="nav-text">git特点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git安装"><span class="nav-number">5.</span> <span class="nav-text">git安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-配置命令"><span class="nav-number">6.</span> <span class="nav-text">git 配置命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#git-config"><span class="nav-number">6.1.</span> <span class="nav-text">git config</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#配置级别"><span class="nav-number">6.1.1.</span> <span class="nav-text">配置级别</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#配置内容"><span class="nav-number">6.1.2.</span> <span class="nav-text">配置内容</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git基本命令"><span class="nav-number">7.</span> <span class="nav-text">git基本命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#初始化仓库"><span class="nav-number">7.1.</span> <span class="nav-text">初始化仓库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看分支状态"><span class="nav-number">7.2.</span> <span class="nav-text">查看分支状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件提交"><span class="nav-number">7.3.</span> <span class="nav-text">文件提交</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除暂存区某个文件提交记录"><span class="nav-number">7.4.</span> <span class="nav-text">删除暂存区某个文件提交记录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件同步到本地仓库"><span class="nav-number">7.5.</span> <span class="nav-text">文件同步到本地仓库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看commit日志"><span class="nav-number">7.6.</span> <span class="nav-text">查看commit日志</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一些工作区命令"><span class="nav-number">7.7.</span> <span class="nav-text">一些工作区命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#查看本地文件和工作区差异"><span class="nav-number">7.7.1.</span> <span class="nav-text">查看本地文件和工作区差异</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#从本地仓库恢复文件"><span class="nav-number">7.7.2.</span> <span class="nav-text">从本地仓库恢复文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#丢弃工作区修改"><span class="nav-number">7.7.3.</span> <span class="nav-text">丢弃工作区修改</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#本地仓库文件的移动和删除"><span class="nav-number">7.8.</span> <span class="nav-text">本地仓库文件的移动和删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#版本控制命令"><span class="nav-number">7.9.</span> <span class="nav-text">版本控制命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#回到之前版本"><span class="nav-number">7.9.1.</span> <span class="nav-text">回到之前版本</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#去往更新的版本"><span class="nav-number">7.9.2.</span> <span class="nav-text">去往更新的版本</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#标签管理"><span class="nav-number">7.10.</span> <span class="nav-text">标签管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建新的标签"><span class="nav-number">7.10.1.</span> <span class="nav-text">创建新的标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#添加标签信息"><span class="nav-number">7.10.2.</span> <span class="nav-text">添加标签信息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#指定某个commit-id打标签"><span class="nav-number">7.10.3.</span> <span class="nav-text">指定某个commit_id打标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#查看标签"><span class="nav-number">7.10.4.</span> <span class="nav-text">查看标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除标签"><span class="nav-number">7.10.5.</span> <span class="nav-text">删除标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#去往某个标签版本"><span class="nav-number">7.10.6.</span> <span class="nav-text">去往某个标签版本</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#临时工作区操作"><span class="nav-number">7.11.</span> <span class="nav-text">临时工作区操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建保存临时工作区"><span class="nav-number">7.11.1.</span> <span class="nav-text">创建保存临时工作区</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#查看保存的工作区"><span class="nav-number">7.11.2.</span> <span class="nav-text">查看保存的工作区</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#应用哪个工作区"><span class="nav-number">7.11.3.</span> <span class="nav-text">应用哪个工作区</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#应用上一个工作区并且删除"><span class="nav-number">7.11.4.</span> <span class="nav-text">应用上一个工作区并且删除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除工作区"><span class="nav-number">7.11.5.</span> <span class="nav-text">删除工作区</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分支操作"><span class="nav-number">7.12.</span> <span class="nav-text">分支操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#什么是分支？"><span class="nav-number">7.12.1.</span> <span class="nav-text">什么是分支？</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#查看当前分支"><span class="nav-number">7.12.2.</span> <span class="nav-text">查看当前分支</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#github"><span class="nav-number">7.13.</span> <span class="nav-text">github</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#添加ssh秘钥"><span class="nav-number">7.13.1.</span> <span class="nav-text">添加ssh秘钥</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#创建新的github仓库"><span class="nav-number">7.13.2.</span> <span class="nav-text">创建新的github仓库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#操作github仓库"><span class="nav-number">7.13.3.</span> <span class="nav-text">操作github仓库</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">  <span class="exturl" data-url="aHR0cDovL3d3dy5iZWlhbi5taWl0Lmdvdi5jbg==">京ICP备18001214号 </span>&copy; 2014 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">任成</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
    <span title="站点总字数">277k</span>
  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.2"></script>

  <script src="/js/motion.js?v=7.1.2"></script>



  
  


  <script src="/js/affix.js?v=7.1.2"></script>

  <script src="/js/schemes/pisces.js?v=7.1.2"></script>



  
  <script src="/js/scrollspy.js?v=7.1.2"></script>
<script src="/js/post-details.js?v=7.1.2"></script>



  


  <script src="/js/next-boot.js?v=7.1.2"></script>


  

  
  <script src="/js/exturl.js?v=7.1.2"></script>


  

  


  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
  

  
  

  


  

  
<script>
if ($('body').find('pre.mermaid').length) {
  $.ajax({
    type: 'GET',
    url: '//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js',
    dataType: 'script',
    cache: true,
    success: function() {
      mermaid.initialize({
        theme: 'forest',
        logLevel: 3,
        flowchart: { curve: 'linear' },
        gantt: { axisFormat: '%m/%d/%Y' },
        sequence: { actorMargin: 50 }
      });
    }
  });
}
</script>


  

  

  

  

  

  

  

  

  

</body>
</html>
